<template>
  <el-dialog
    ref="refDlg"
    v-loading="loading"
    title="终止"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="600px"
    @close="closeDialog"
  >
    <div>
      <el-form ref="refForm" class="financeForm" :rules="formRules" :model="formData" size="mini" label-width="100px">
        <el-row>
          <el-col v-if="detailData.flag === 'stop'" :span="24">
            <el-form-item label="客户经理:" size="mini" prop="manager" :rules="[{ required: detailData.flag === 'stop', message: '请选择客户经理' }]">
              <input-with-dialog
                v-model="formData.manager"
                :props="{label:'userName',value:'account'}"
                :multiple="false"
                :config="managerConfig"
                @change="managerChange"
              />
            </el-form-item>
          </el-col>
          <el-col v-if="detailData.flag === 'stop'" :span="24">
            <el-form-item label="是否涉及扣款:" size="mini" prop="isRelateDeduction" :rules="[{ required: detailData.flag === 'stop', message: '请选择' }]">
              <el-select v-model="formData.isRelateDeduction" placeholder="请选择" style="width: 100%">
                <el-option v-for="item in booleansIntList" :key="item.dictCode" :label="item.dictName" :value="item.dictCode" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="formData.isRelateDeduction" :span="24">
            <!-- 测绘费扣款、设计费扣款 -->
            <el-form-item
              label="扣款类型:"
              size="mini"
              prop="deductionType"
              :rules="[{ required: formData.isRelateDeduction, message: '请选择' }]"
            >
              <el-checkbox-group v-model="formData.deductionType">
                <el-checkbox label="测绘费扣款">测绘费扣款</el-checkbox>
                <el-checkbox label="设计费扣款">设计费扣款</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="终止原因:" size="mini" prop="exceptionDesc">
              <el-input v-model="formData.exceptionDesc" type="textarea" placeholder="请输入终止原因" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
// import { saveCostCenter } from '@/api/costCenter'
// import { getUserInfo } from '@/utils/auth'
import { submitMarketApply, getExceptionType } from '@/api/storeConstruction'
export default {
  name: 'DemoEditForm',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      readonly: false,
      storeList: [],
      customer: [],
      booleansIntList: [
        { dictCode: 1, dictName: '是' },
        { dictCode: 0, dictName: '否' }
      ], // 判断是否
      formData: {
        exceptionDesc: '',
        deductionType: []
      },
      queryObj: {
        shopExceptionDetailDTOS: []
      },
      formRules: {
        exceptionDesc: [
          { required: true, message: '请输入终止原因' }
        ]
      },
      managerConfig: {
        title: '客户经理',
        filtersRaw: [{ id: 'passwordValidWay', property: 'passwordValidWay', operator: '=', value: 2 }],
        interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/user/domainMarketingUser/page`,
        setting: [
          {
            prop: 'account',
            title: '编码'
          },
          {
            prop: 'userName',
            title: '名称'
          },
          {
            prop: 'orgCode',
            title: '所属组织编码'
          },
          {
            prop: 'orgName',
            title: '所属组织名称'
          }
        ]
      }
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  async created() {
    this.queryObj = JSON.parse(JSON.stringify(this.detailData))
    const obj = [{ 'id': 'exceptionType', 'property': 'exceptionType', 'operator': '=', 'value': '终止项目' }]
    // const shopFilter = [{ 'id': 'code', 'property': 'code', 'operator': '<>', 'value': this.formData.beforeShopCode },
    //   { 'id': 'agencyCode', 'property': 'agencyCode', 'operator': '=', 'value': this.detailData.officeCode }]
    // this.$set(this.shopDialogConfig, 'filtersRaw', shopFilter)
    const dataForm = new FormData()
    dataForm.append('filtersRaw', JSON.stringify(obj))
    await getExceptionType(dataForm).then(res => {
      if (res.success && res.data && res.data.records) {
        this.exceptionHandle = res.data.records[0]
        this.queryObj.exceptionHandleId = this.exceptionHandle.id
        this.queryObj.modelId = this.queryObj.id
      }
    })
    // this.formData.exceptionDesc = ''
  },

  methods: {
    // 选择客户经理
    managerChange(data) {
      if (data && data.length) {
        this.queryObj.customerManagerCode = data[0].account
        this.queryObj.customerManagerName = data[0].userName
      } else {
        this.queryObj.customerManagerCode = ''
        this.queryObj.customerManagerName = ''
      }
    },
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        exceptionDesc: ''
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    // 提交表单处理
    submitForm() {
      // if (this.submiting) return
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          if (this.formData.isRelateDeduction) this.formData.deductionType = this.formData.deductionType.join(',')
          else this.formData.deductionType = ''
          this.loading = true
          this.submiting = true
          delete this.formData.manager
          const shopExceptionDetailDTOS = []
          shopExceptionDetailDTOS.push(this.formData)
          this.queryObj.shopExceptionDetailDTOS = shopExceptionDetailDTOS
          this.queryObj.bizCode = this.queryObj.constCode
          this.queryObj.exceptionDesc = this.formData.exceptionDesc
          submitMarketApply(this.queryObj).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.closeDialog()
              const isNew = !this.formData.id // 是否新增数据 true (新增则刷新列表到第一页)/ false (非新增则刷新当前页)
              this.$emit('success', isNew)
            }
            this.loading = false
          }).catch(() => {
            this.submiting = false
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
.img-upload-container {
  width: fit-content;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
  .icon-msg{
    margin-left: 10px;
    color: #999;
  }
}
</style>
